﻿using System.Web;
using msBsc.AppLogic.DataAlc;
using System.Web.SessionState;
using System.Security.Principal;
using System.Collections;

namespace msBsc.Cls
{
    public class imageHandler : IHttpHandler, IRequiresSessionState
    {
        public imageHandler()
        {
        }

        public void ProcessRequest(HttpContext context)
        {

            string token = @context.Request["COOKATHID"];

            if (token != null)
            {
                token = msBsc.AppLogic.Cfg.DesCry.uncMe(token, "cookesc");
                System.Web.Security.FormsAuthenticationTicket ticket = System.Web.Security.FormsAuthentication.Decrypt(token);

                if (ticket != null)
                {
                    System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(ticket);
                    string[] roles = { "*" }; //System.Web.Security.Roles.GetRolesForUser(identity.Name); 注意这里，如果你没有启用asp.net权限系统，会报错。
                    System.Security.Principal.GenericPrincipal principal = new GenericPrincipal(identity, roles);
                    context.User = principal;
                }
            }


            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("image", "gif,jpg,jpeg,png,bmp");
            extTable.Add("flash", "swf,flv");
            extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
            extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

            context.Response.ContentType = "text/plain";
            context.Response.Charset = "GB2312";

            HttpPostedFile file = context.Request.Files["Filedata"];
            //string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
            string strParentId = context.Request.Params.Get("ParentId");
            string strDirNme = context.Request.Params.Get("DirNme");
            bool blIsDir = context.Request.Params.Get("IsDir") == "1";
            string strFileId = context.Request.Params.Get("FileId");
            string strRelId = context.Request.Params.Get("RelId");
            string strType1 = context.Request.Params.Get("rType1");
            string strType2 = context.Request.Params.Get("rType2");
            if (string.IsNullOrEmpty(strType1))
            {
                strType1 = "1";
            }

            if (file != null)
            {
                using (CBizSysFile ooo = new CBizSysFile())
                {
                    //0EA51FE9ZZ1X1 未分类
                    context.Response.Write(ooo.SaveImgToServer(file, strFileId, strRelId, strType1, strType2, "", strParentId, blIsDir,""));//"0EA51FE9ZZ1X1"
                }
                //if (File.Exists(uploadPath + file.FileName))
                //{
                //    context.Response.Write("3");            //文件已经存在
                //    return;
                //}

                //string[] fn = file.FileName.Split('.');
                //string ext = fn[fn.Length - 1];
                //string filename = msBsc.SvRef.ApplicationConst.Const.GenerateStrId() + "." + ext;
                //if (!Directory.Exists(uploadPath))
                //{
                //    Directory.CreateDirectory(uploadPath);
                //}
                //file.SaveAs(uploadPath + filename);
                ////下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                //// context.Session[context.Session["userName"].ToString()] = filename;       //这里书写有点不规范
                //SaveImgToServer(fn[0], "", context.Request["folder"] + "/" + filename, ext, file.ContentLength);
                //context.Response.Write(filename);
            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        //public void SaveImgToServer(string strFileName, string strServerHttp, string strVpath, string strFileExt, int iFileSize)
        //{
        //    using (CBizSysFile objSysFile = new CBizSysFile())
        //    {
        //        DataSet dsSysFile = objSysFile.SelectEndImg();
        //        int iFileNo = 0;
        //        if (dsSysFile.Tables[0].Rows.Count > 0)
        //        {
        //            iFileNo = Convert.ToInt32(dsSysFile.Tables[0].Rows[dsSysFile.Tables[0].Rows.Count - 1]["FILE_NO"].ToString()) + 1;
        //        }
        //        DataRow drSysFile = dsSysFile.Tables[0].NewRow();
        //        drSysFile["FILE_ID"] = (new Epm.AppLogic.Framework.CDbAccess()).NewID();
        //        drSysFile["USER_NME"] = msBsc.SvRef.UserInfo.Operator;
        //        drSysFile["RELATION_TYPE_CID2"] = "none";
        //        drSysFile["FILE_NO"] = (iFileNo).ToString();
        //        drSysFile["FILE_NME"] = strFileName;
        //        drSysFile["SERVER_HTTP"] = strServerHttp;
        //        drSysFile["FILE_V_PATH"] = strVpath;
        //        drSysFile["FILE_EXT"] = strFileExt;
        //        drSysFile["FILE_SIZE"] = iFileSize;

        //        dsSysFile.Tables[0].Rows.Add(drSysFile);

        //        if (dsSysFile.HasChanges())
        //        {
        //            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
        //            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
        //            transactionOption.Timeout = new TimeSpan(0, 10, 0);
        //            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
        //            {
        //                objSysFile.Update(dsSysFile);
        //                ts.Complete();
        //                ts.Dispose();
        //            }
        //        }
        //    }
        //}



    }
}